package com.edou.cms.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.cms.dao.ICmsArticleDao;
import com.edou.cms.dao.ICmsColumnArticleDao;
import com.edou.cms.entity.CmsArticle;
import com.edou.cms.entity.CmsColumnArticle;
import com.edou.cms.service.ICmsColumnArticleService;

import com.edou.cms.dto.CmsColumnArticleCondition;

@Service("cmsColumnArticleServiceImpl")
@Transactional(value = "transactionManager")
public class CmsColumnArticleServiceImpl implements ICmsColumnArticleService{

	@Resource(name = "cmsColumnArticleDaoImpl")
	private ICmsColumnArticleDao cmsColumnArticleDao;
	@Resource(name = "cmsArticleDaoImpl")
	private ICmsArticleDao cmsArticleDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listCmsColumnArticleByPage(PageResult<CmsColumnArticle> pageResult,CmsColumnArticleCondition condition){
		int rows = cmsColumnArticleDao.listCmsColumnArticleByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<CmsColumnArticle> list = cmsColumnArticleDao.listCmsColumnArticleByPage(rowBounds,condition);
		pageResult.setResult(list);
	}
	
	/**
	 * 查询动态报道
	 */
	public Map<String, List<CmsArticle>> listCmsColumnArticleDtbdByPageAndMonth(PageResult<CmsColumnArticle> pageResult,CmsColumnArticleCondition condition) {
		int rows = cmsColumnArticleDao.listCmsColumnArticleDtbdByPageAndMonthCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<String> list = cmsColumnArticleDao.listCmsColumnArticleDtbdByPageAndMonth(rowBounds,condition);
		Map<String,List<CmsArticle>> map = new TreeMap<String,List<CmsArticle>>(new Comparator<String>(){
		   public int compare(String obj1,String obj2){
		    //降序排序
		    return obj2.compareTo(obj1);
		   }
		}); 
		for (String key : list) {
			List<CmsArticle> cmsArticle = cmsArticleDao.listArticleDtbdByYearMonth(key,condition.getColumnId());
			map.put(key, cmsArticle);
		}
		return map;
	}
	

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 专栏内的相关文章
	 */
	public CmsColumnArticle getCmsColumnArticleById(String id){
		return cmsColumnArticleDao.getCmsColumnArticleById(id);
	}

	/**
	 * 新增
	 * @param item 专栏内的相关文章
	 */
	public void add(CmsColumnArticle item){
		cmsColumnArticleDao.add(item);
	}

	/**
	 * 修改
	 * @param item 专栏内的相关文章
	 */
	public void update(CmsColumnArticle item){
		CmsColumnArticle cmsArticle = cmsColumnArticleDao.getCmsColumnArticleById(item.getId());
		if(StringUtils.isNotBlank(item.getArticleId())){
			cmsArticle.setArticleId(item.getArticleId());
		}
		if(null != item.getColumnSection()){
			cmsArticle.setColumnSection(item.getColumnSection());
		}
		if(StringUtils.isNotBlank(item.getColumnId())){
			cmsArticle.setColumnId(item.getColumnId());
		}
		if(null != item.getSort()){
			cmsArticle.setSort(item.getSort());
		}
		cmsColumnArticleDao.update(cmsArticle);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		cmsColumnArticleDao.delByIds(ids);
	}

	/**
	 * 批量添加
	 */
	public void addArticleList(String[] articleIds, String columnId,
			Integer sectionId) {
		for (String articleId : articleIds) {
			CmsColumnArticle item = new CmsColumnArticle();
			item.setArticleId(articleId);
			item.setColumnId(columnId);
			item.setColumnSection(sectionId);
			cmsColumnArticleDao.add(item);
		}
	}
}

